﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IDotNet.MapLibs.Web.Model;
using Newtonsoft.Json;

namespace IDotNet.MapLibs.Web.Pages
{
    public partial class MapDrawAreaPage : BasePage, IMapDrawArea
    {
        private IMapDrawArea mapDrawArea;
        protected string mapReqJson;
        protected void Page_Load(object sender, EventArgs e)
        {
            mapDrawArea = (IMapDrawArea)this;
            mapDrawArea.MapDrawArea();
        }

        Model.MapDrawAreaRequestModel IMapDrawArea.GetRequestParam()
        {
            return GetParam<MapDrawAreaRequestModel>();
        }

        bool IMapDrawArea.CheckRequestParam(Model.MapDrawAreaRequestModel request, out string errMsg)
        {
            if (request == null)
            {
                errMsg = "请求参数为空";
                return false;
            }
            if (request.ReturnUrl == null)
            {
                errMsg = "回调页面为空";
                return false;
            }
            if (!request.mapBelongs.Any())
            {
                errMsg = "mapBelongs为空";
                return false;
            }
            if (request.mapBelongs.Select(p => p.mapId).Distinct().Count() != request.mapBelongs.Count)
            {
                errMsg = "mapId不能重复";
                return false;
            }

            errMsg = null;
            return true;
        }

        Model.MapDrawAreaResultModel IMapDrawArea.MapDrawArea()
        {
            var selectMapReq = mapDrawArea.GetRequestParam();

            string checkErrMsg;
            if (mapDrawArea.CheckRequestParam(selectMapReq, out checkErrMsg))
            {
                mapReqJson = ToJsonString(selectMapReq);
            }
            else
            {
                ShowMsg(checkErrMsg);
            }
            return null;
        }
    }
}